/**
 * Copyright (C), 2011-2017
 * File Name: BaseController.java
 * Encoding: UTF-8
 * Date: 17-9-11 下午4:34
 * History:
 */
package com.swak.frame.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.swak.frame.dto.request.RpcParamRequest;
import com.swak.frame.logger.SwakLoggerFactory;
import com.swak.frame.util.DFormatEnum;
import com.swak.frame.util.DateTimeUtils;
import com.swak.frame.util.GetterUtil;
import com.swak.frame.util.NetUtils;

/**
 * ClassName: BaseController.java
 * 
 * @author colley.ma
 * @date 2021年3月17日 下午3:51:15
 */
public abstract class BaseController {
    protected final Logger logger = SwakLoggerFactory.getLogger(getClass());

    protected static final String NONE_PERMISSION = "error/nonePermission";
    protected static final String EXCEPTION = "error/exception";

    protected HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
            .getRequest();
    }

    protected HttpServletResponse getResponse() {
        return new ServletWebRequest(
            ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest())
                .getResponse();
    }

    protected Long getParamLong(String name) {
        String param = getRequest().getParameter(name);

        return GetterUtil.getLong(param, null);
    }

    protected String getParamString(String name) {
        return getRequest().getParameter(name);
    }

    protected String getParamString(String name, String defaultValue) {
        String param = getRequest().getParameter(name);
        return GetterUtil.getString(param, defaultValue);
    }

    protected List<Long> getParamLongs(String param) {
        if (StringUtils.isNotEmpty(param)) {
            return GetterUtil.getSplit2Long(param);
        }

        return new ArrayList<Long>();
    }

    protected Integer getParamInteger(String name) {
        String param = getRequest().getParameter(name);

        return GetterUtil.getInteger(param, null);
    }

    protected Integer getParamInteger(String name, Integer defaultVal) {
        String param = getRequest().getParameter(name);

        return GetterUtil.getInteger(param, defaultVal);
    }

    protected List<Integer> getParamIntegers(String name) {
        String param = getRequest().getParameter(name);

        if (StringUtils.isNotEmpty(param)) {
            return GetterUtil.getSplit2Integer(param);
        }

        return new ArrayList<Integer>();
    }

    protected Date getParamDate(String paramName, String fmt) {
        if (StringUtils.isEmpty(fmt)) {
            fmt = DFormatEnum.YYYY_MM_DD.pattern;
        }

        String endDateStr = getRequest().getParameter(paramName);

        if (StringUtils.isNotEmpty(endDateStr)) {
            return DateTimeUtils.string2Date(endDateStr, fmt);
        }

        return null;
    }

    protected Date getParamStartDate(String paramName) {
        String endDateStr = getRequest().getParameter(paramName);
        if (StringUtils.isNotEmpty(endDateStr)) {
            endDateStr = endDateStr + " 00:00:00";
            Date endDate =
                DateTimeUtils.string2Date(endDateStr, DFormatEnum.YYYY_MM_DDHH_MM_SS.pattern);
            if (endDate != null) {
                return endDate;
            }
        }
        return null;
    }

    protected Date getParamEndDate(String paramName) {
        String endDateStr = getRequest().getParameter(paramName);
        if (StringUtils.isNotEmpty(endDateStr)) {
            Date endDate = DateTimeUtils.string2Date(endDateStr, DFormatEnum.YYYY_MM_DD.pattern);
            if (endDate != null) {
                endDate = DateUtils.addHours(endDate, 23);
                endDate = DateUtils.addMinutes(endDate, 59);
                endDate = DateUtils.addSeconds(endDate, 59);
                return new Date(endDate.getTime());
            }
        }
        return null;
    }

    protected RpcParamRequest getRpcParamRequest() {
        String currentPin = getParamString("pin");
        String token = getParamString("token");
        Map<String, String> bizPara = Maps.newHashMap();
        Map<String, String[]> reqParams = getRequest().getParameterMap();
        if (MapUtils.isNotEmpty(reqParams)) {
            for (Entry<String, String[]> entry : reqParams.entrySet()) {
                String key = entry.getKey();
                String[] val = entry.getValue();
                String paramVal = "";
                if (ArrayUtils.isNotEmpty(val)) {
                    paramVal = val[0];
                }
                bizPara.put(key, paramVal);
            }
        }
        bizPara.put("token", token);
        Map<String, Object> requestParam = Maps.newHashMap();
        requestParam.put("pin", currentPin);

        String requestIp = NetUtils.getClientIpAddr(getRequest());
        requestParam.put("ip", requestIp);

        requestParam.putAll(bizPara);
        requestParam.put("body", JSON.toJSONString(bizPara));
        // 其它信息
        return RpcParamRequest.getRpcParamRequest(requestParam);
    }

    protected Map<String, Object> getParamRequestMap() {
        String currentPin = getParamString("pin");
        String token = getParamString("token");
        Map<String, String> bizPara = Maps.newHashMap();
        Map<String, String[]> reqParams = getRequest().getParameterMap();
        if (MapUtils.isNotEmpty(reqParams)) {
            for (Entry<String, String[]> entry : reqParams.entrySet()) {
                String key = entry.getKey();
                String[] val = entry.getValue();
                String paramVal = "";
                if (ArrayUtils.isNotEmpty(val)) {
                    paramVal = val[0];
                }
                bizPara.put(key, paramVal);
            }
        }
        bizPara.put("token", token);
        Map<String, Object> requestParam = Maps.newHashMap();
        requestParam.put("pin", currentPin);

        String requestIp = NetUtils.getClientIpAddr(getRequest());
        requestParam.put("ip", requestIp);

        requestParam.putAll(bizPara);
        requestParam.put("body", JSON.toJSONString(bizPara));
        // 其它信息
        return requestParam;
    }

}
